package sll;

import sllvm.IntAndTable;
import sllvm.Table;

public class OpExp extends Exp
{
	public Exp left, right;
	public int oper;
	final public static int Plus=1, Minus=2, Times=3, Div=4;
	
	public OpExp(Exp l, int o, Exp r) 
	{
		left=l; oper=o; right=r;
	}

	@Override
	public IntAndTable interpretate(Table t)
	{	
		int res;
		
		//excutando expressao interna
		IntAndTable it1 = left.interpretate(t);
		IntAndTable it2 = right.interpretate(it1.table);
		
		// atualizando a tabela
		t = it2.table;
		
		// executando operacao
		switch(oper)
		{
			case Plus:
				res = it1.integer + it2.integer;
			break;
			
			case Minus:
				res = it1.integer - it2.integer;
			break;
			
			case Times:
				res = it1.integer * it2.integer;
			break;
			
			case Div:
				res = it1.integer / it2.integer;
			break;
			
			default:
				res = 0;
				System.out.println("OpExp: Operador invalido.");
		}
		
		
		return new IntAndTable(t, res);
	}
}